﻿using GlobalData;
using Microsoft.Extensions.DependencyInjection;
using Model.Core.Entity.Dbcontexts;
using Model.Core.Services;
using Newtonsoft.Json.Linq;
using RobotPratolSolution.Models;
using System.Collections.ObjectModel;

namespace RobotPratolSolution.ViewModels
{
    public class TaskListViewModel
    {
        DbcontextBase _DbcontextBase { get; set; }


        CloudMqClientCtrl _MqClientCtrl { get; set; }
        MqTopics _GlobalMqTopics { get; set; }
        LocalRobotInfo LocalRobotInfo { get; set; }
        public TaskListViewModel()
        {
            _DbcontextBase = App.ServiceProvider.GetService<DbcontextBase>();
            JobModels = new ObservableCollection<JobInfoModel>();

            LocalRobotInfo = App.ServiceProvider.GetService<LocalRobotInfo>();
            _GlobalMqTopics = App.ServiceProvider.GetService<MqTopics>();
            _MqClientCtrl = App.ServiceProvider.GetService<CloudMqClientCtrl>();
        }
        public void StartJob(int index)
        {
            if (index >= 0)
            {
                JObject jsondata = new JObject();
                string jobid = JobModels[index].Id.ToString();
                jsondata.Add("command", "start_job");
                jsondata.Add("jobid", jobid);
              _=   _MqClientCtrl.SendMessageAsync(_GlobalMqTopics.Job_Ctrl_Topic, jsondata.ToString());
            }
        }
        public void StopJob()
        {
            //string jobid = JobModels[index].Id.ToString();
            JObject jsondata = new JObject();
            jsondata.Add("command", "stop_job");
          _=  _MqClientCtrl.SendMessageAsync(_GlobalMqTopics.Job_Ctrl_Topic, jsondata.ToString());
        }


        public void AddJobInfo(string Name)
        {
            string JobType = "StackerRobot";
            Guid JobId = Guid.NewGuid();
            JobModels.Add(new JobInfoModel()
            {
                Name = Name,
                Id = JobId.ToString(),
                JobType = JobType

            });
            _DbcontextBase.JobInfo.Add(new Model.Core.Entity.JobInfo()
            {
                Name = Name,
                JobType = JobType,
                Id = JobId,
                RobotId = LocalRobotInfo.RobotId
            }); ;
            _DbcontextBase.SaveChanges();
            //string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "TaskList.json");
            //File.WriteAllText(filepath, JsonConvert.SerializeObject(TaskModels));
        }

        public void UpdateJobProperty(int index, string name)
        {
            JobInfoModel jobInfoModel = JobModels[index];
            var job = _DbcontextBase.JobInfo.FirstOrDefault(a => a.Id.ToString() == jobInfoModel.Id);
            job.Name = name; jobInfoModel.Name = name;
            _DbcontextBase.JobInfo.Update(job);
            _DbcontextBase.SaveChanges();
        }
        public void DeleteTask(int Index)
        {
            JobInfoModel jobInfoModel = JobModels[Index];
            var job = _DbcontextBase.JobInfo.FirstOrDefault(a => a.Id.ToString() == jobInfoModel.Id);
            var jobnodes = _DbcontextBase.JobNodeInfo.Where(a => a.jobid.ToString() == jobInfoModel.Id);
            var jobcronnodes = _DbcontextBase.JobCroninfo.Where(a => a.JobId.Id.ToString() == jobInfoModel.Id);
            _DbcontextBase.JobInfo.Remove(job);
            _DbcontextBase.JobNodeInfo.RemoveRange(jobnodes);
            _DbcontextBase.JobCroninfo.RemoveRange(jobcronnodes);
            _DbcontextBase.SaveChanges();
            JobModels.RemoveAt(Index);
        }
        public void RefreshTask()
        {
            JobModels.Clear();
            var datalist = _DbcontextBase.JobInfo.Where(a => a.RobotId == LocalRobotInfo.RobotId).ToList();
            foreach (var item in datalist)
            {
                JobModels.Add(new JobInfoModel()
                {
                    Name = item.Name,
                    Id = item.Id.ToString(),
                    JobType = item.JobType,
                });
            }
            ;
            //string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "TaskList.json");
            //if (File.Exists(filepath))
            //{
            //    string Taskconfig = File.ReadAllText(filepath);
            //    if (!string.IsNullOrEmpty(Taskconfig) && Taskconfig.Length > 5)
            //        TaskModels = JsonConvert.DeserializeObject<ObservableCollection<TaskModel>>(Taskconfig);
            //}
        }

        public ObservableCollection<JobInfoModel> JobModels { get; set; }
    }
}
